﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace xmgl.Common
{
    public class FilterBaseAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //获取菜单数据，判断该登录人是否有权限
            //判断菜单权限使用数据库数据，
            //先判断有没有菜单权限（Controller），再判断有没有按钮权限（Action）
            string path = filterContext.RequestContext.HttpContext.Request.Path;
            string[] ArrPath = path.Split('/');
            if (ArrPath.Length >= 3 && ArrPath[2] != "Login")
            {
                object obj = filterContext.RequestContext.HttpContext.Session[Common.Constant.LoginedUser];
                if (obj != null)
                {
                    string userName = obj.ToString();
                    List<string> menuList = new List<string>() { "Home" };//查询用户名对应的菜单权限
                    bool b = menuList.Exists(menu => menu == ArrPath[1]);

                    if (b)
                    {
                        //filterContext.Controller.ViewBag.mess1 = "正要加载" + filterContext.ActionDescriptor.ActionName + "方法1";
                        base.OnActionExecuting(filterContext);
                        return;
                    }
                }
                filterContext.HttpContext.Response.Redirect("~/Home/Login");
            }
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //可以自行添加一些控制代码  
            //filterContext.Controller.ViewBag.mess2 = filterContext.ActionDescriptor.ActionName + "方法加载完毕";
            base.OnActionExecuted(filterContext);
        }
    }
}